package net.coding.bamboo.wex.fundmanage;

import com.fasterxml.jackson.core.type.TypeReference;
import com.weihui.basic.lang.common.util.DateUtil;
import com.weihui.basic.util.marshaller.json.JsonUtil;
import com.weihui.commom.agreement.enums.DurationType;
import com.weihui.commom.agreement.enums.RepayMode;
import com.weihui.finance.fundmanage.client.request.*;
import com.weihui.finance.fundmanage.client.response.LoanResponse;
import com.weihui.finance.fundmanage.client.response.PayResponse;
import com.weihui.finance.fundmanage.client.response.PrepayResponse;
import com.weihui.finance.fundmanage.client.util.FundManageClient;
import com.weihui.finance.fundmanage.core.common.enums.InterestType;
import com.weihui.finance.fundmanage.core.common.enums.PayModel;
import com.weihui.finance.fundmanage.core.common.utils.ObjectMapperUtils;
import com.weihui.finance.fundmanage.domain.common.response.BaseResponse;
import net.coding.bamboo.wex.fundmanage.model.Customer;
import org.junit.BeforeClass;
import org.junit.Test;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author : zenghua
 * @version : v0.1 FundManageSecurityClientTest.java
 * @since : 2017/7/11 15:18
 */
public class FundManageSecurityClientTest {
    private static FundManageClient fundManageClient;

    @BeforeClass
    public static void beforeClass() {
        fundManageClient = new FundManageClient();
        //dev
//        fundManageClient.setBaseAddress("http://10.65.178.14:9404/fund-manage");
        //test，
//        fundManageClient.setBaseAddress("http://10.65.209.16:9404/fund-manage");
        //cloud
//        fundManageClient.setBaseAddress("http://10.65.213.11:22/fund-manage");

        fundManageClient.setBaseAddress("http://127.0.0.1:8080");
    }

    @Test
    public void test() {
        String uuid = UUID.randomUUID().toString();
        System.out.println(uuid);
    }

    /**
     * 助贷放款
     */
    @Test
    public void testLoanConfirm() {
        PayConfirmRequest payConfirmRequest = new PayConfirmRequest();
        payConfirmRequest.setBatchNo("201707131200-0007");
        payConfirmRequest.setFundPartnerId("200006238080");
        fundManageClient.setUrl("/finance/fundManage/pay/confirm");

        TypeReference<BaseResponse> baseResponseTypeReference = new TypeReference<BaseResponse>() {};
        BaseResponse baseResponse = fundManageClient.invoke(payConfirmRequest, null, baseResponseTypeReference);
        String resp = JsonUtil.toJson(baseResponse);
        System.out.println(resp);
    }

    @Test
    public void testLoan() {
        LoanRequest loanRequest = new LoanRequest();
        loanRequest.setApplyNo("7210555564083106");
        loanRequest.setFundPartnerId("200003670082");
        loanRequest.setChannelCode("KSRCB");

        //会员,1122会员ID 10000001428392
        loanRequest.setMemberId("10000001428803");
        //商户ID，查询会员必填
        loanRequest.setPartnerId("200005493433");
        //身份证密文
        loanRequest.setCertNo("P405652");
        //姓名密文
        loanRequest.setCustomerName("P405653");
        loanRequest.setLoanAmount(new BigDecimal("5000"));
        loanRequest.setLoanTerm(1);
        loanRequest.setTermUnit(DurationType.DAY.name());
        //卡ID 294507，295140
        loanRequest.setLoanCardNo("295928");
        loanRequest.setRepaymentCardNo("295928");
        loanRequest.setInterestType(InterestType.FLOAT);
        loanRequest.setFixedInterestRate(new BigDecimal("6"));
        loanRequest.setFlowInterestRate(new BigDecimal("7"));
        //TODO 修改时间
        loanRequest.setApplyDate("20170831091839");
        //TODO
        loanRequest.setBillStartDate("20170831");
        loanRequest.setRepayMode(RepayMode.EQUIVALENT_PRINCIPAL.name());
        loanRequest.setLoanPurpose("客户信息加密5");
        loanRequest.setLoanCardBankId("KSRCB");//102305223298
        loanRequest.setRepaymentCardBankId("KSRCB");
        loanRequest.setOverdueRate(new BigDecimal("0.08"));
        loanRequest.setRepaymentDay(2);

        Customer customer = new Customer();
        customer.setName("赵四");
        customer.setAge(24);
        loanRequest.setCustomerInfo(ObjectMapperUtils.objectToJsonString(customer));


        TypeReference<LoanResponse> loanResponseTypeReference = new TypeReference<LoanResponse>() {};
        LoanResponse loanResponse = fundManageClient.invoke(loanRequest, null, loanResponseTypeReference);
        String resp = JsonUtil.toJson(loanResponse);
        System.out.println(resp);
//        String json = "{\"returnCode\":null,\"returnMessage\":null,\"extension\":null,\"redirectForm\":null,\"loanNo\":\"97895a39-e4b0-4659-b654-5d6789bfacc7\"}";
//        System.out.println(JsonUtil.parse(json, loanResponseTypeReference));
    }

    /**
     * 异步测试
     */
    @Test
    public void testLoan2() {
        LoanRequest loanRequest = new LoanRequest();
        loanRequest.setApplyNo("621055558446822");
        loanRequest.setFundPartnerId("200006238080");
//        loanRequest.setChannelCode("KSRCB1");

        //会员
        loanRequest.setMemberId("10000001428708");
        //商户ID，查询会员必填
        loanRequest.setPartnerId("200005497016");
        //身份证密文
        loanRequest.setCertNo("P4134897");
        //姓名密文
        loanRequest.setCustomerName("P1336168");
        loanRequest.setLoanAmount(new BigDecimal("5000"));
        loanRequest.setLoanTerm(1);
        loanRequest.setTermUnit(DurationType.DAY.name());
        //卡ID 294507，295140
        loanRequest.setLoanCardNo("295714");
        loanRequest.setRepaymentCardNo("295714");
        loanRequest.setInterestType(InterestType.FLOAT);
        loanRequest.setFixedInterestRate(new BigDecimal("6"));
        loanRequest.setFlowInterestRate(new BigDecimal("7"));
        loanRequest.setApplyDate(DateUtil.format(new Date(), DateUtil.longFormat));
        loanRequest.setBillStartDate("20170807");
        loanRequest.setRepayMode(RepayMode.EQUIVALENT_PRINCIPAL.name());
        loanRequest.setLoanPurpose("客户信息加密5");
        loanRequest.setLoanCardBankId("CCB");//102305223298
        loanRequest.setRepaymentCardBankId("CCB");
        loanRequest.setOverdueRate(new BigDecimal("0.08"));
        loanRequest.setRepaymentDay(2);
        Customer customer = new Customer();
        customer.setName("赵四123");
        customer.setAge(25);
        loanRequest.setCustomerInfo(ObjectMapperUtils.objectToJsonString(customer));


        TypeReference<LoanResponse> loanResponseTypeReference = new TypeReference<LoanResponse>() {};
        LoanResponse loanResponse = fundManageClient.invoke(loanRequest, null, loanResponseTypeReference);
        String resp = JsonUtil.toJson(loanResponse);
        System.out.println(resp);
//        String json = "{\"returnCode\":null,\"returnMessage\":null,\"extension\":null,\"redirectForm\":null,\"loanNo\":\"97895a39-e4b0-4659-b654-5d6789bfacc7\"}";
//        System.out.println(JsonUtil.parse(json, loanResponseTypeReference));
    }


    /**
     * 普通还款：
     * HTWL30520601710000013001
     * HTWL30520601710000014001
     *
     * 7/10
     * HTWL30520601710000025001
     */
    @Test
    public void testPay() {
        PayRequest payRequest = new PayRequest();
        payRequest.setFundPartnerId("200003670082");
        payRequest.setChannelCode("KSRCB");
        List<RefundRecord> refundRecords = new ArrayList<>();
        RefundRecord refundRecord = new RefundRecord();
        refundRecord.setLoanNo("HTWL30520601710000032001");
        refundRecord.setPayMode(PayModel.NORMAL.name());
        refundRecord.setPayAmount(new BigDecimal("1000"));
        refundRecord.setIssueNumber(1);
        refundRecord.setRepaymentTime(DateUtil.format(new Date(), DateUtil.longFormat));
        //手续费
//        refundRecord.setTipFee(new BigDecimal("200"));
        refundRecords.add(refundRecord);

//        refundRecord = new RefundRecord();
//        refundRecord.setLoanNo("HTWL305206-1831331787001");
//        refundRecord.setPayMode(PayModel.NORMAL.name());
//        refundRecord.setPayAmount(new BigDecimal("20"));
//        refundRecord.setIssueNumber(6);
//        refundRecord.setRepaymentTime(DateUtil.format(new Date(), DateUtil.longFormat));
//        refundRecords.add(refundRecord);

        payRequest.setRefundRecords(refundRecords);

        TypeReference<PayResponse> typeReference = new TypeReference<PayResponse>() {};

        PayResponse payResponse = fundManageClient.invoke(payRequest, null, typeReference);
        System.out.println(JsonUtil.toJson(payResponse));
    }

    /**
     * 提前还款试算
     */
    @Test
    public void testPrePay() {
        PrepayRequest prepayRequest = new PrepayRequest();
//        prepayRequest.setChannelCode("KSRCB");
//        prepayRequest.setPrepayAmount(new BigDecimal("10000"));
        prepayRequest.setFundPartnerId("200003670082");
        prepayRequest.setPayMode(PayModel.PREPAYMENT.name());
        prepayRequest.setLoanNo("HTWL30520601710000102001");
        prepayRequest.setIssueNumber(1);

        TypeReference<PrepayResponse> typeReference = new TypeReference<PrepayResponse>() {};

        PrepayResponse prepayResponse = fundManageClient.invoke(prepayRequest, null, typeReference);
        System.out.println(JsonUtil.toJson(prepayResponse));
    }
}
